package com.sdtbu.ylgh.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sdtbu.ylgh.utils.JwtHelper;
import com.sdtbu.ylgh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sdtbu.ylgh.entity.PatientEntity;
import com.sdtbu.ylgh.service.PatientService;



/**
 * 患者表
 *
 * @author mzd
 * @email 1695165447@qq.com
 * @date 2021-03-13 17:53:04
 */
@CrossOrigin
@RestController
@RequestMapping("ylgh/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;

    /**
     * 根据 token 获取更登陆成功的用户信息，然后查询他的 就诊人列表
     */
    @RequestMapping("/list")
    public R list(@RequestHeader(value="token", defaultValue = "") String token){
        Map<String, Object> params = new HashMap<>();
        if(token == "") {
            return R.error("未登录！");
        }
        params.put("user_id", JwtHelper.getUserId(token));
        List<PatientEntity> lists = patientService.listByMap(params);

        return R.ok().put("list", lists);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ylgh:patient:info")
    public R info(@PathVariable("id") Long id){
		PatientEntity patient = patientService.getById(id);

        return R.ok().put("patient", patient);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ylgh:patient:save")
    public R save(@RequestBody PatientEntity patient){
        System.out.println("patient:" + patient);
        patientService.save(patient);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ylgh:patient:update")
    public R update(@RequestBody PatientEntity patient){
		patientService.updateById(patient);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ylgh:patient:delete")
    public R delete(@RequestBody Long[] ids){
		patientService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
